package com.vf.admin.utils.douyin;

import com.vf.admin.utils.douyin.entity.FanHui;

import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 *抖音音乐主页获取无水印视频 
 */
public class YinYueZhuYeUtils {
    private String charset = "utf-8";  
    private HttpClientUtil httpClientUtil = null;  
      
    public YinYueZhuYeUtils(){  
        httpClientUtil = new HttpClientUtil();  
    }  
      
    public String test(String url){  
        String httpOrgCreateTest = url + "httpOrg/create";  
        Map<String,String> createMap = new HashMap<String,String>();
        	
        createMap.put("Host","api.amemv.com"); 
        createMap.put("Connection","keep-alive"); 
        createMap.put("Cookie","odin_tt=f774a2e8a2e319031c621510fae1e9112f9b84a5cae34e6fc2ac0868cb7a91080e5542c252d7d687d6361662a067e1e7; sid_guard=ea6991be99cee16d3faf1ff28c5d55f8%7C1545138740%7C5184000%7CSat%2C+16-Feb-2019+13%3A12%3A20+GMT; uid_tt=1e88cc5560c85719458b3021825f9214; sid_tt=ea6991be99cee16d3faf1ff28c5d55f8; sessionid=ea6991be99cee16d3faf1ff28c5d55f8; install_id=56986543517; ttreq=1$cac0c9c22ef0f2c5061b5d0573bae3f6727ac059"); 
        createMap.put("Accept-Encoding","gzip"); 
//        createMap.put("X-SS-REQ-TICKET","1546918804767"); 
        createMap.put("X-Tt-Token","00ea6991be99cee16d3faf1ff28c5d55f8d6a5de97be39c0f182d2339875a6e155541697e3874598d5dbcba2d73991bb5a54"); 
        createMap.put("sdk-version","1"); 
        createMap.put("X-SS-RS","0"); 
        createMap.put("User-Agent","com.ss.android.ugc.aweme/400 (Linux; U; Android 8.1.0; zh_CN; Redmi 6; Build/O11019; Cronet/58.0.2991.0)"); 
        createMap.put("X-Khronos","1546920855"); 
        createMap.put("X-Gorgon","01cc1b78f899167da7ae828e766dc15bdfe9c42c3aeba1469e"); 
        createMap.put("X-Pods",""); 
        String httpOrgCreateTestRtn = httpClientUtil.doPost(httpOrgCreateTest,createMap,charset);  
        
        System.out.println(httpOrgCreateTestRtn);  
        return httpOrgCreateTestRtn;
    }  
      
//    public static void main(String[] args){
//    	YinYueZhuYeUtils zhuYeMain = new YinYueZhuYeUtils();
//    	for(int i=0;i<10000;i++){
//    		int e = 20;
////    		zhuYeMain.request(i*e);
//    		System.out.println("--------------------------------------------------------");
//    	}
//
//    }
    
    /**
     * 请求列表
     */
    public String convert(String yinyueid,int count){
    	try{
    		String url = "https://api.amemv.com/aweme/v1/music/aweme/?music_id="+yinyueid+"&cursor="+count+"&count=20&type=6&retry_type=no_retry&mcc_mnc=46007&iid=56949813016&device_id=61618700661&ac=wifi&channel=aweGW&aid=1128&app_name=aweme&version_code=400&version_name=4.0.0&device_platform=android&ssmix=a&device_type=SM-G955N&device_brand=samsung&language=zh&os_api=19&os_version=4.4.2&uuid=354730011701392&openudid=1708bcd4f9667762&manifest_version_code=400&resolution=720*1280&dpi=240&update_version_code=4002&_rticket=1547636673499&ts=1547636673&as=a1a5c06341cc7c5f0f4699&cp=01c9cf5b18f038fae1ckko&mas=01ee2f0785c537195d58d123bccc1f5c759c9c6c2c660c66acc6ec";
    	
    		List<FanHui> fanHuiList = new ArrayList<FanHui>();
        	YinYueZhuYeUtils main = new YinYueZhuYeUtils();  
            String result = main.test(url);  
            return result;
//            DouYinShiPin douyinShiPin = JSON.parseObject(result,DouYinShiPin.class);
//            List<Aweme> awemeList = douyinShiPin.getAweme_list();
//            String palyurlStr = "";
//            String gequming = "";
//            if(awemeList!=null && awemeList.size()>0){
//            	for(Aweme aweme : awemeList){
//            		 String ickname =  aweme.getAuthor().getNickname();
//                     String signature = aweme.getAuthor().getSignature();
//                     String uid = aweme.getAuthor().getUid();
//                      String verify = aweme.getAuthor().getCustom_verify();
//                      String userid = aweme.getAuthor_user_id();
//                      String miaoshu = aweme.getDesc();
//                      Music music = aweme.getMusic();
//                      if(music!=null){
//                    	  gequming = aweme.getMusic().getTitle();
//                    	  PlayUrl palyurl = music.getPlay_url();
//                    	  if(palyurl!=null){
//                    		  List urlist = palyurl.getUrl_list();
//                    		  if(urlist!=null && urlist.size()>0){
//                    			  palyurlStr = urlist.get(0)+"";
//                    		  }
//                    	  }
//                      }
//                      Video video = aweme.getVideo();
//                      String vediourl = "";
//                      if(video!=null){
//                    	  PlayAddres payaddress = video.getPlay_addr();
//                    	  if(payaddress!=null){
//                    		  List playUrlList = payaddress.getUrl_list();
//                              if(playUrlList!=null && playUrlList.size()>0){
//                            	  vediourl = playUrlList.get(0).toString();
//                              }
//                    	  }
//                      }
//                      System.out.println("ickname:"+ickname+"  signature:"+signature+"   uid:"+uid+"   verify:"+verify+"   userid:"+userid+"  palyurlStr:"+palyurlStr);
//                      FanHui fanhui = new FanHui();
//                      fanhui.setZuozhe(filterEmoji(ickname));
//                      fanhui.setMiaoshu(filterEmoji(miaoshu));
//                      fanhui.setDownloadurl(vediourl);
//                      fanhui.setGequming(filterEmoji(gequming));
//                      fanHuiList.add(fanhui);
//            	}
//            }
    	}catch(Exception e){
    		e.printStackTrace();
    	}
    	return null;
    }
    
    public String tiaoZhuanUrl(String url) {  
        try {  
            System.out.println("访问地址:" + url);  
            URL serverUrl = new URL(url);  
            HttpURLConnection conn = (HttpURLConnection) serverUrl  
                    .openConnection();  
            conn.setRequestMethod("GET");  
            // 必须设置false，否则会自动redirect到Location的地址  
            conn.setInstanceFollowRedirects(false);  
  
            conn.addRequestProperty("Accept-Charset", "UTF-8;");  
            conn.addRequestProperty("User-Agent",  
                    "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.8) Firefox/3.6.8");  
            conn.addRequestProperty("Referer", "http://zuidaima.com/");  
            conn.connect();  
            String location = conn.getHeaderField("Location");  
  
            serverUrl = new URL(location);  
            conn = (HttpURLConnection) serverUrl.openConnection();  
            conn.setRequestMethod("GET");  
  
            conn.addRequestProperty("Accept-Charset", "UTF-8;");  
            conn.addRequestProperty("User-Agent",  
                    "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.8) Firefox/3.6.8");  
            conn.addRequestProperty("Referer", "http://zuidaima.com/");  
            conn.connect();  
            System.out.println("跳转地址:" + location);  
            return location;
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return null;
    }  
    
    /**
     * 将emoji表情替换成空串
     *  
     * @param source
     * @return 过滤后的字符串
     */ 
    public String filterEmoji(String source) { 
     if (source != null && source.length() > 0) { 
      return source.replaceAll("[\ud800\udc00-\udbff\udfff\ud800-\udfff]", ""); 
     } else { 
      return source; 
     } 
    }
    
}